<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Resources extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');	
	}
	
	public function index()
	{
		$data['module'] = "user";
		$data['page'] = 'layout_resources';
		$this->load->view($this->layout_content,$data);	
	}
	
	public function form()
	{
		$this->crud->set_table('sys_resources');
		$this->crud->set_subject('Data resources');
		$this->crud->set_primary_key('id');
		$this->crud->required_fields('name');
		$this->crud->columns('name');

		$this->crud->unset_read();
		$this->crud->unset_add_fields('id','module','orderby');
		$this->crud->unset_edit_fields('id','module','orderby');	
		
		$this->crud->callback_add_field('parent_id',array($this,'callback_add_parent_id'));		
		$this->crud->callback_edit_field('parent_id',array($this,'callback_edit_parent_id'));		
		$this->crud->callback_edit_field('name',array($this,'callback_plaintext_name'));
		
		$this->crud->unset_back_to_list();
		
		$output = $this->crud->render();
		
		$data['output'] = $output;
		$data['page'] = "layout_resources_form";
		$data['module'] = "user";
		$this->load->view($this->layout_content,$data);	
	}
	
	public function callback_plaintext_name($value,$primary_key)
	{
		return $value;
	}
	
	public function callback_add_parent_id()
	{
		$resources = $this->user_model->get_array_resources();
		$html  = "<select name=\"parent_id\">\n";			
		$html .= $this->user_model->get_option_resources($resources);
		$html .= "</select>";
		return $html;
	}
	
	public function callback_edit_parent_id($value,$primary_key)
	{
		$resources = $this->user_model->get_array_resources();
		$html  = "<select name=\"parent_id\">\n";			
		$html .= $this->user_model->get_option_resources($resources,1,$value);
		$html .= "</select>";
		return $html;
	}
	
	public function data()
	{
		$permission = $this->user_model->get_array_resources();
		$this->output->set_content_type('application/json')->set_output(json_encode($permission));	
	}
	
	
	
}